package es.ats.codington.festival.junit;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.People;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.DAOException;


import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.PlaceService;
import es.ats.codington.festival.services.VisitorService;
import es.ats.codington.festival.utils.FERSDataConnection;
/**
 * 
 * @author JAVA101_01
 *
 */

public class TEventSignUpService extends TestCase {

	EventSignUpService esservice;
	VisitorService vservice;
	EventService eservice;
	PlaceService pservice;
	Place place1, place;
	People p1, people;
	Visitor v1, visitor;
	Event event2, event;
	EventSignUp eventSignUp1;
	
	
	 /**
	  * SetUp of the test
	  * @throws Exception
	  */
	@Before
	public void setUp() throws Exception {
		super.setUp();
		
		esservice = new EventSignUpService();
		vservice = new VisitorService();
		eservice = new EventService();
		pservice = new PlaceService();
		
	}

	/**
	 * TearDown of the test
	 * @throws Exception
	 */
	@After
	public void tearDown() throws Exception {
		super.tearDown();
		
	}

	
	/**
	 * Test for the RegisteredVisitorToEvent method
	 */
	@Test
	public void testRegisterVisitorToEvent() {
		
		boolean affectedRows;
		
		People p1 = new People();
		p1.setFirstName("Jose");
		p1.setLastName("Martinez Nieto");
		p1.setDni("32.888.932-Y");
		p1.setEmail("jose.martinez.nieto@gmail.com");
		p1.setPhoneNumber("689658945");
		p1.setAddress("C/Gran Via");
		
		
		vservice.createVisitor(p1);
	
		
		Visitor v1 = new Visitor();
		v1.setUserName("Jose1");
		v1.setPassword("josemola33");
		v1.setAdmin(false);
		v1.setVisitorId(p1.getIdPeople());
	
		vservice.createVisitorV(v1);
		
		Place place1 = new Place();
		place1.setNamePlace("Codington Arena");
		place1.setCapacityPlace(100000);
		place1.setPlaceDescription("Football Stadium");
		place1.setZonePlace("NORTH");
		place1.setImagePlace("");
		
		pservice.insertPlace(place1, "Stadium");
		
	
		Event event2 = new Event();
		
		event2.setName("Madrid-Barcelona");
		event2.setDescription("Football match");
		event2.setPlace("Bernabeu");
		event2.setStartEvent("20:45");
		event2.setEndEvent("22:30");
		event2.setDayEvent("06/Marzo/2015");
		event2.setEventType("Sports");
		event2.setSeatsAvailable(90);
		event2.setIdPlace(place1.getId());
		
		eservice.insertEvent(event2);
	
		
        EventSignUp eventSignUp1 = new EventSignUp();
		
		//eventSignUp1.setIdEvent(event.getEventID());
		//eventSignUp1.setIdVisitor(visitor.getVisitorId());
        eventSignUp1.setIdEvent(event2.getEventID());
        eventSignUp1.setIdVisitor(v1.getVisitorId());
		
		affectedRows = esservice.registerVisitorToEvent(eventSignUp1);
		
		
		
		assertTrue(affectedRows);
		
		try {
			//FERSDataConnection.commit(conn);
			//FERSDataConnection.closeConnection(conn);
		}	catch (DAOException ex) {}
	}

	/**
	 * Test for the CheckEventsofVisitor method
	 */
	 @Test
	  public void testCheckEventsofVisitor () {
		
		boolean check;
		
		
		People p1 = new People();
		p1.setFirstName("Manuel");
		p1.setLastName("Perez Nieto");
		p1.setDni("32.868.932-Y");
		p1.setEmail("manuel.pereznieto@gmail.com");
		p1.setPhoneNumber("689658945");
		p1.setAddress("C/Gran Via");
		
		
		vservice.createVisitor(p1);
	
		
		Visitor v1 = new Visitor();
		v1.setUserName("mapeni");
		v1.setPassword("mapeni33");
		v1.setAdmin(false);
		v1.setVisitorId(p1.getIdPeople());
	
		vservice.createVisitorV(v1);
		
		Place place1 = new Place();
		place1.setNamePlace("Nou Camp");
		place1.setCapacityPlace(100000);
		place1.setPlaceDescription("Footbal Stadium");
		place1.setZonePlace("NORTH");
		place1.setImagePlace("");
		
		pservice.insertPlace(place1, "Stadium");
		
		Event event2 = new Event();
		
		event2.setName("Madrid-Atleti");
		event2.setDescription("Football match");
		event2.setPlace("Bernabeu");
		event2.setStartEvent("20:45");
		event2.setEndEvent("22:30");
		event2.setDayEvent("06/Marzo/2015");
		event2.setEventType("Sports");
		event2.setSeatsAvailable(90);
		event2.setIdPlace(place1.getId());
		
		eservice.insertEvent(event2);
		
		
		EventSignUp eventSignUp1 = new EventSignUp();
		
		
		eventSignUp1.setIdEvent(event2.getEventID());
        eventSignUp1.setIdVisitor(v1.getVisitorId());
		
	
        esservice.registerVisitorToEvent(eventSignUp1);
		
        check = esservice.checkEventsofVisitor(eventSignUp1);
        
		
		assertTrue(check);
		
		try {
			
		}	catch (DAOException ex) {}
	}
	
	 /**
	 * Test for the CheckRegisteredEvents method
	 */
	@Test
	public void testCheckRegisteredEvents () {
		
		People p1 = new People();
		p1.setFirstName("Pedro");
		p1.setLastName("Diez Nieto");
		p1.setDni("32.868.922-Y");
		p1.setEmail("pedro.dieznieto@gmail.com");
		p1.setPhoneNumber("689658935");
		p1.setAddress("C/Gran Via");
		
		
		vservice.createVisitor(p1);
	
		
		Visitor v1 = new Visitor();
		v1.setUserName("pedini");
		v1.setPassword("pedini33");
		v1.setAdmin(false);
		v1.setVisitorId(p1.getIdPeople());
	
		vservice.createVisitorV(v1);
		
		Place place1 = new Place();
		place1.setNamePlace("Santiago Bernabeu");
		place1.setCapacityPlace(100000);
		place1.setPlaceDescription("Footbal Stadium");
		place1.setZonePlace("NORTH");
		place1.setImagePlace("");
		
		pservice.insertPlace(place1, "Stadium");
		
		
		Event event2 = new Event();
		
		event2.setName("Madrid-Granada");
		event2.setDescription("Football match");
		event2.setPlace("Bernabeu");
		event2.setStartEvent("20:45");
		event2.setEndEvent("22:30");
		event2.setDayEvent("06/Marzo/2015");
		event2.setEventType("Sports");
		event2.setSeatsAvailable(90);
		event2.setIdPlace(place1.getId());
		
		eservice.insertEvent(event2);
		
		
		EventSignUp eventSignUp1 = new EventSignUp();
		
		
		eventSignUp1.setIdEvent(event2.getEventID());
        eventSignUp1.setIdVisitor(v1.getVisitorId());
		
	
        esservice.registerVisitorToEvent(eventSignUp1);
		
		
		List<Event> events = new ArrayList<Event>();
		
		eventSignUp1.setIdVisitor(v1.getVisitorId());
		events = esservice.registeredEvents(eventSignUp1);
		
		assertEquals(events.size(), 1);
		
		try {
			//FERSDataConnection.commit(conn);
		}	catch (DAOException ex) {}
	}
	
	/**
	 * Test for the UnregisteredVisitorToEvent method
	 */
	@Test
	public void testUnregisterVisitorToEvent() throws SQLException {
		boolean affectedRows;
		
		People p1 = new People();
		p1.setFirstName("Daniel");
		p1.setLastName("Perez Alejo");
		p1.setDni("32.828.932-Y");
		p1.setEmail("daniel.perezalejo@gmail.com");
		p1.setPhoneNumber("689628945");
		p1.setAddress("C/Gran Via");
		
		
		vservice.createVisitor(p1);
	
		
		Visitor v1 = new Visitor();
		v1.setUserName("dapeal");
		v1.setPassword("dapeal33");
		v1.setAdmin(false);
		v1.setVisitorId(p1.getIdPeople());
	
		vservice.createVisitorV(v1);
		
		Place place1 = new Place();
		place1.setNamePlace("La Catedral");
		place1.setCapacityPlace(100000);
		place1.setPlaceDescription("Footbal Stadium");
		place1.setZonePlace("NORTH");
		place1.setImagePlace("");
		
		pservice.insertPlace(place1, "Stadium");
		
		Event event2 = new Event();
		
		event2.setName("Madrid-Sevilla");
		event2.setDescription("Football match");
		event2.setPlace("Bernabeu");
		event2.setStartEvent("20:45");
		event2.setEndEvent("22:30");
		event2.setDayEvent("06/Marzo/2015");
		event2.setEventType("Sports");
		event2.setSeatsAvailable(90);
		event2.setIdPlace(place1.getId());
		
		eservice.insertEvent(event2);
		
		
		EventSignUp eventSignUp1 = new EventSignUp();
		
		
		eventSignUp1.setIdEvent(event2.getEventID());
        eventSignUp1.setIdVisitor(v1.getVisitorId());
		
	
        esservice.registerVisitorToEvent(eventSignUp1);
		
		affectedRows = esservice.unregisterVisitorToEvent(eventSignUp1);
		assertTrue(affectedRows);
		
		try {
			
		}	catch (DAOException ex) {}
		
		
	}

}
